package com.ld.oauth2.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * HttpClient4.3
 * 
 * @author hang.luo
 */
public class HttpClientUtils {
	private static Logger logger = LoggerFactory
			.getLogger(HttpClientUtils.class);

	private static RequestConfig requestConfig = null;

	static {
		requestConfig = RequestConfig.custom().setSocketTimeout(2000)
				.setConnectTimeout(2000).build();
	}

	public static JSONObject httpPost(String url, JSONObject jsonParam) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		JSONObject jsonResult = null;
		HttpPost httpPost = new HttpPost(url);
		httpPost.setConfig(requestConfig);
		try {
			if (null != jsonParam) {
				StringEntity entity = new StringEntity(jsonParam.toString(),
						"utf-8");
				entity.setContentEncoding("UTF-8");
				entity.setContentType("application/json");
				httpPost.setEntity(entity);
			}
			CloseableHttpResponse result = httpClient.execute(httpPost);
			if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String str = "";
				try {
					str = EntityUtils.toString(result.getEntity(), "utf-8");
					jsonResult = JSONObject.parseObject(str);
				} catch (Exception e) {
					logger.error("post request error:" + url, e);
				}
			}
		} catch (IOException e) {
			logger.error("post request error:" + url, e);
		} finally {
			httpPost.releaseConnection();
		}
		return jsonResult;
	}

	public static JSONObject httpPost(String url, String strParam) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		JSONObject jsonResult = null;
		HttpPost httpPost = new HttpPost(url);
		httpPost.setConfig(requestConfig);
		try {
			if (null != strParam) {
				StringEntity entity = new StringEntity(strParam, "utf-8");
				entity.setContentEncoding("UTF-8");
				entity.setContentType("application/json");
				httpPost.setEntity(entity);
			}
			CloseableHttpResponse result = httpClient.execute(httpPost);
			int code = result.getStatusLine().getStatusCode();
			System.out.println("code==>" + code);
			System.out.println("HttpStatus.SC_OK==>" + HttpStatus.SC_OK);
			System.out.println("HttpStatus.SC_FORBIDDEN==>"
					+ HttpStatus.SC_FORBIDDEN);
			if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String str = "";
				try {
					str = EntityUtils.toString(result.getEntity(), "utf-8");
					jsonResult = JSONObject.parseObject(str);
				} catch (Exception e) {
					logger.error("post request error:" + url, e);
				}
			} else if (result.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN) {
				String str = "";
				try {
					str = EntityUtils.toString(result.getEntity(), "utf-8");
					jsonResult = JSONObject.parseObject(str);
				} catch (Exception e) {
					logger.error("post request error:" + url, e);
				}
			}
		} catch (IOException e) {
			logger.error("post request error:" + url, e);
		} finally {
			httpPost.releaseConnection();
		}
		return jsonResult;
	}
	public static JSONObject httpGet(String url) {
		JSONObject jsonResult = null;
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet request = new HttpGet(url);
		request.setConfig(requestConfig);
		try {
			CloseableHttpResponse response = client.execute(request);

			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = response.getEntity();
				String strResult = EntityUtils.toString(entity, "utf-8");
				jsonResult = JSONObject.parseObject(strResult);
			} else {
				logger.error("get request error:" + url);
			}
		} catch (IOException e) {
			logger.error("get request error:" + url, e);
		} finally {
			request.releaseConnection();
		}
		return jsonResult;
	}

	public static void main(String[] args) {
		String url = "http://139.198.13.48:8885/api/sync/factory";
		Map<String, Object> obj = new HashMap<String, Object>();
//		obj.put("ddyId", 26);
//		obj.put("efName", "aaa0");
//		obj.put("groupId", 3);
//		obj.put("pointList", "1000,200");
//		obj.put("pointType", 0);
//		obj.put("efRemark", "");
		try {
			System.out.println(obj);
			System.out.println();
			JSONObject jsonObj = (JSONObject)JSONObject.toJSON(obj);
			JSONObject str = HttpClientUtils.httpPost(url, jsonObj);
			System.out.println(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}